<template>
  <div class="content">
    <Top
      @orderCreate="orderCreate"
      :all="allGoodsNum"
      :num="selectNum"
      :price="selectPrice"
    />
    <div class="theader">
      <div class="all">
        <div class="sel">
          <el-checkbox
            :indeterminate="allIndeterminate"
            :value="allSelect"
            @change="allShopSelect"
          ></el-checkbox>
        </div>
        全选
      </div>
      <div class="goods">商品</div>
      <div class="spec">规格</div>
      <div class="price">单价</div>
      <div class="num">数量</div>
      <div class="money">金额</div>
      <div class="action">操作</div>
    </div>
    <div class="shop-item" v-for="(item, index) in cartlist" :key="index">
      <div class="shop-tit">
        <div class="select">
          <el-checkbox
            :indeterminate="shopIndeterminate(index)"
            :value="shopAllSelect(index)"
            @change="shopSelect(index)"
          ></el-checkbox>
        </div>
        <img :src="item.shop_img" alt="" @click="toShopPage(item.shop_id)" />
        <div class="title" @click="toShopPage(item.shop_id)">
          {{ item.shop_name }}
        </div>
        <CouponList :list="item.coupon" />
        <!-- <div class="coupon-list swiper-container">
          <div class="swiper-wrapper">
            <div class="swiper-slide">
              <CouponItem
                :coupon="coupon"
                v-for="(coupon, i) in item.coupon"
                @change="coupon.status = 1"
                :key="i"
              />
              <CouponItem
                :coupon="coupon"
                v-for="coupon in item.coupon"
                @change="coupon.status = 1"
                :key="coupon.coupon_id"
              />
            </div>
          </div>
        </div> -->
        <icon
          @click.native="showCouponInfo(item)"
          name="iconxiangyoubeifen16"
          size="16"
        />
      </div>
      <div class="goods">
        <div class="goods-item" v-for="(goods, i) in item.good" :key="i">
          <div class="select">
            <el-checkbox
              :value="cartselect[index].goods[i]"
              @change="goodsSelect(index, i)"
            ></el-checkbox>
          </div>
          <img :src="goods.banner" alt="" />
          <div class="title">{{ goods.name }}</div>
          <div
            class="spec"
            v-html="goods.specification_name.split(' ').join('<br/>')"
          ></div>
          <div class="price">
            <b>￥{{ goods.price }}</b>
            <!-- <s>￥{{ goods.del_price }}</s> -->
          </div>
          <div class="num">
            <!-- <NumberInput1 v-model="goods.num" :max="goods.stock" /> -->
            <div class="box">
              <el-input-number
                @change="goodsQuantity(goods)"
                v-model="goods.goods_quantity"
                size="mini"
                :min="goods.minimum_number || 1"
              ></el-input-number>
            </div>
            <span>库存 {{ goods.stock }}</span>
          </div>
          <div class="money">
            ￥{{ (goods.goods_quantity * goods.price).toFixed(2) }}
          </div>
          <div class="action">
            <span @click="favoGoods(goods)">移入收藏夹</span>
            <span @click="removeGoods(index, i)">删除</span>
          </div>
        </div>
      </div>
    </div>
    <YouLike1 />
  </div>
</template>

<script>
import { mapState } from "vuex";
import api from "@/api";
import Top from "./top";
import CouponList from "./couponlist";
// import CouponItem from "@/components/CouponItem/CouponItem.vue";
import YouLike1 from "@/components/YouLike/YouLike1.vue";

export default {
  name: "CarIndex",
  components: { Top, YouLike1, CouponList },
  data() {
    return {
      allIndeterminate: false,
      allSelect: false,
      selectNum: 0,
      selectPrice: 0,
    };
  },
  computed: {
    ...mapState("car", ["cartlist", "cartselect"]),
    allGoodsNum() {
      let num = 0;
      if (!this.cartlist) return num;
      this.cartlist.forEach((item) => {
        num += item.good.length;
      });
      return num;
    },
  },
  async created() {
    await this.$store.dispatch("car/init");
  },
  methods: {
    // 判断店铺是否半选
    shopIndeterminate(index) {
      let length = this.cartselect[index].goods.filter((item) => item).length;
      return length > 0 && length < this.cartselect[index].goods.length;
    },
    // 判断店铺是否全选
    shopAllSelect(index) {
      let length = this.cartselect[index].goods.filter((item) => item).length;
      return length === this.cartselect[index].goods.length;
    },
    allShopSelect(status) {
      this.$store.commit("car/allShopSelect", status);
      this.$forceUpdate();
      this.renderAllSelect();
    },
    shopSelect(index) {
      this.$store.commit("car/shopSelect", {
        index,
        select: !this.shopAllSelect(index),
      });
      this.$forceUpdate();
      this.renderAllSelect();
    },
    goodsSelect(index, i) {
      this.$store.commit("car/goodsSelect", { index, i });
      this.$forceUpdate();
      this.renderAllSelect();
    },
    renderAllSelect() {
      let indeterminate = false;
      this.cartselect.some((item, index) => {
        indeterminate = this.shopIndeterminate(index);
        return indeterminate;
      });
      this.allIndeterminate = indeterminate;

      let select = true;
      this.cartselect.some((item, index) => {
        select = this.shopAllSelect(index);
        return !select;
      });
      this.allSelect = select;

      (this.selectNum = 0), (this.selectPrice = 0);
      this.cartlist.forEach((item, index) => {
        item.good.forEach((goods, i) => {
          let status = this.cartselect[index].goods[i];
          if (status) {
            this.selectNum += 1;
            this.selectPrice += goods.goods_quantity * parseFloat(goods.price);
          }
        });
      });
    },
    renderGoodsList() {
      let goodsList = [];
      this.goodsNum = 0;
      this.goodsPrice = 0;
      this.cartselect.forEach((item, index) => {
        let shop = !!item.goods.filter((item) => item).length;
        if (shop) {
          shop = Object.assign({}, this.cartlist[index], { good: [] });
          item.goods.forEach((status, i) => {
            if (status) {
              this.goodsNum += 1;
              let goods = this.cartlist[index].good[i];
              this.goodsPrice += goods.goods_quantity * goods.price;
              shop.good.push(goods);
            }
          });
          goodsList.push(shop);
        }
      });
      return goodsList;
    },
    async removeGoods(index, i) {
      this.$confirm("是否删除该商品?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store.dispatch("car/removeGoods", { index, i });
          this.$forceUpdate();
          this.renderAllSelect();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async favoGoods(goods) {
      await api.GOODS_COLLECTION({
        id: [goods.good_id],
        status: goods.goods_collection ? 0 : 1,
      });
      this.$message.success("收藏成功");
    },
    async goodsQuantity(goods) {
      await api
        .CAR_ADD_DESC({
          cart_id: goods.cart_id,
          goods_quantity: goods.goods_quantity,
        })
        .catch(() => {
          goods.goods_quantity--;
        });
      this.$forceUpdate();
      this.renderAllSelect();
    },
    orderCreate() {
      if (this.selectNum) {
        let shopList = this.renderGoodsList();
        api
          .ORDER_GOODS({
            type: 0,
            cartgood: shopList,
          })
          .then((res) => {
            this.$router.push({ name: "OrderCreate" });
            console.log(res);
          });
        // console.log(this.selectNum);
      } else {
        this.$message.error("请先选择商品");
      }
    },
    // 获取店铺优惠券
    showCouponInfo(item) {
      console.log(item);
      // if (item.coupon.length) {
      //   this.Popups.ShopCoupon().show(item, () => {
      //     console.log(11111);
      //   });
      // }
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  background: #fff;
  padding: 29px 0 1px;
  .theader {
    height: 42px;
    background: #f3f5f9;
    box-sizing: border-box;
    border: 1px solid #f0f0f0;
    max-width: 1200px;
    margin: 19px auto 20px;
    display: flex;
    align-items: center;
    padding: 0 17px;
    font-size: 16px;
    color: #333333;
    .all {
      display: flex;
      align-items: center;
      .sel {
        width: 16px;
        height: 16px;
        background: #d52c1a;
        border-radius: 5px;
        color: #fff;
        margin-right: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      width: 64px;
      margin-right: 69px;
      cursor: pointer;
    }
    .goods {
      width: 269px;
    }
    .spec {
      width: 160px;
    }
    .price {
      width: 120px;
    }
    .num {
      width: 201px;
    }
    .money {
      width: 160px;
    }
  }
  .shop-item {
    max-width: 1200px;
    margin: 20px auto;
    box-sizing: border-box;
    .shop-tit {
      padding: 0 0 0 17px;
      display: flex;
      align-items: center;
      .select {
        width: 16px;
        height: 16px;
        margin-right: 16px;
      }
      img {
        width: 28px;
        height: 28px;
        border-radius: 5px;
      }
      .title {
        font-size: 16px;
        font-weight: 600;
        color: #333333;
        padding: 0 16px 0 12px;
      }
    }
    .goods {
      margin-top: 21px;
      background: #f9f9f9;
      border-radius: 5px;
      border: 1px solid #dedede;
      padding: 16px 16px 0;
      .goods-item {
        display: flex;
        margin-bottom: 20px;
        .select {
          width: 16px;
          height: 16px;
          background: #d52c1a;
          border-radius: 5px;
          color: #fff;
          margin-right: 16px;
          display: flex;
          align-items: center;
          justify-content: center;
        }
        img {
          width: 78px;
          height: 78px;
          border-radius: 5px;
        }
        .title {
          font-size: 14px;
          color: #333333;
          line-height: 20px;
          width: 240px;
          margin: 0 40px 0 12px;
        }
        .spec {
          width: 120px;
          margin-right: 40px;
          font-size: 14px;
          color: #666666;
          line-height: 20px;
        }
        .price {
          display: flex;
          flex-direction: column;
          font-size: 14px;
          font-weight: 600;
          color: #333333;
          line-height: 20px;
          width: 80px;
          margin-right: 39px;
          s {
            color: #999999;
          }
        }
        .num {
          width: 120px;
          margin-right: 81px;
          .box {
            width: 116px;
            ::v-deep .el-input-number--mini {
              width: 116px;
            }
            ::v-deep .el-input__inner,
            ::v-deep .is-disabled {
              background: transparent;
            }
          }
          span {
            font-size: 14px;
            color: #666666;
            line-height: 20px;
            margin-top: 8px;
          }
        }
        .money {
          width: 80px;
          margin-right: 80px;
          font-size: 14px;
          font-weight: 600;
          color: #d52c1a;
          line-height: 20px;
        }
        .action {
          display: flex;
          flex-direction: column;
          font-size: 14px;
          color: #333333;
          line-height: 20px;
          span {
            display: inline-block;
            margin-bottom: 10px;
            cursor: pointer;
          }
        }
      }
    }
  }

  .you-like {
    margin: 40px auto;
    width: 1200px;
  }
}
</style>
